package org.wmine.preprocess;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

 class Missing extends JFrame implements ActionListener
	{
		JLabel lblselect,lbltext;
		JComboBox j1,j2;
		JTextField t1;
		
		JButton cancel,addBtn,ok;
		/*String Col[]={"RollNo","Name"};
		String values[][]={{"1","?"},{"?","Puja"}};
		//ArrayList l1=new ArrayList();
		String ss=values[0][1].concat("abc");*/
		DefaultTableModel dtm;
		public Missing(DefaultTableModel dtm)
		{
			this.dtm=dtm;
			setBounds(200,200,400,230);
			setLayout(null);
			setVisible(true);
			setTitle("Replace with global value");
			
			lblselect=new JLabel("Select Attribute");
			lblselect.setBounds(50,50,100,25);
			
			j1=new JComboBox();
			for(int i=0;i<this.dtm.getColumnCount();i++)
			{
				j1.addItem(this.dtm.getColumnName(i));
			}
			j1.setBounds(200,50,150,25);
			
			
			lbltext=new JLabel("Enter Global value");
			lbltext.setBounds(50,85,150,25);
			
			t1=new JTextField(20);
			t1.setBounds(200,85,150,25);
			
			addBtn=new JButton("Replace");
			addBtn.addActionListener(this);
			addBtn.setBounds(40,130,100,25);
			
			ok=new JButton("Exit");
			ok.addActionListener(this);
			ok.setBounds(150,130,100,25);
			
			cancel=new JButton("Cancel");
			cancel.addActionListener(this);
			cancel.setBounds(260,130,100,25);
			
			add(lblselect);
			add(lbltext);
			add(j1);
			add(t1);
			add(cancel);
			add(ok);
			add(addBtn);
			
		}
		public void actionPerformed(ActionEvent e)
		{
			
			if(e.getSource()==cancel)
				this.dispose();
			else if(e.getSource()==addBtn)
			{
				PreprocessPanel.report.append("<br>You chose to replace missing values with default values<br>");
				if(t1.getText()==null)
				{
					JOptionPane.showMessageDialog(null,"Plese enter a global value");	
				}
				else
				{
					//ArrayList l1=new ArrayList();
					String l1=t1.getText().trim();
					//l1.add(t1.getText());
					//System.out.println((String)j1.getSelectedItem());
					t1.setText("");
					funct1(j1.getSelectedIndex(),l1);
					
				}
			}
			else if(e.getSource()==ok)
			{
				this.dispose();
			}
			//System.out.println("PreprocessPanel.report:"+PreprocessPanel.report);
		}
		public void funct1(int colName,String l1)
		{
			System.out.println("col name=="+colName);
			org.wmine.preprocess.Binning b=new org.wmine.preprocess.Binning();	
			if(b.ContainsMissingValues(this.dtm,colName)==true)
			{
				for(int i=0;i<this.dtm.getRowCount();i++)
				{
					if(this.dtm.getValueAt(i,colName).toString().trim().equals("?") || this.dtm.getValueAt(i,colName).toString().trim().equals("") || this.dtm.getValueAt(i,colName).toString().trim().equals(null))
					{
						this.dtm.setValueAt(l1,i,colName);
					}
				
				}
				JOptionPane.showMessageDialog(null,"Replace Successful!!");
				PreprocessPanel.report.append("Default Value for <b>"+j1.getSelectedItem().toString()+" : "+l1+"</b><br>");
				PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
				PreprocessPanel.summary.repaint();
			}
			else
				JOptionPane.showMessageDialog(null,"Column doesnt contain missing values!! Cannot replace!!");
			//if containsMissingValues() then setText() to arrayList
		}
	}
	
 class Mean_Median1 extends JFrame implements ActionListener
	{
		JLabel lblselect;
		JComboBox j1;
		JButton btnOK;
		String title=null;
		DefaultTableModel dtm;
		public Mean_Median1(String title,DefaultTableModel dtm) 
		{
			this.dtm=dtm;
			this.title=title;
			setTitle(this.title);
			setLayout(null);
			this.setBounds(200,200,400,200);
			
			lblselect=new JLabel("Select Attribute");
			lblselect.setBounds(50,50,90,25);
			
			j1=new JComboBox();
			for(int i=0;i<dtm.getColumnCount();i++)
			{
				j1.addItem(dtm.getColumnName(i));
			}
			j1.setBounds(160,50,150,25);
			
			btnOK=new JButton("OK");
			btnOK.setBounds(130,85,100,25);
			btnOK.addActionListener(this);
			add(lblselect);
			add(j1);
			add(btnOK);
			
			setVisible(true);
			
		}
		public void actionPerformed(ActionEvent ae)
		{
			int row=this.dtm.getRowCount()-1;
			int col=j1.getSelectedIndex();
			Binning b=new Binning();
			PreprocessPanel.report.append("<br>You chose to replace missing values with Mean Value<br>");
			PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
			PreprocessPanel.summary.repaint();
			if(ae.getSource()==btnOK && this.title.equals("Replace by Mean"))
			{
				if(b.ContainsMissingValues(this.dtm,col)==true)
				{
					Algorithm algo=new Algorithm(this.dtm,col);
					float mean=algo.CalculateMean1(0,row);
					if(mean!=-1)
					{
						for(int i=0;i<this.dtm.getRowCount();i++)
						{
							if(this.dtm.getValueAt(i,col).toString().trim().equals("?") || this.dtm.getValueAt(i,col).toString().trim().equals("") || this.dtm.getValueAt(i,col).toString().trim().equals(null))
							{
								this.dtm.setValueAt(mean,i,col);
							}
						
						}
						JOptionPane.showMessageDialog(null,"Replace Successful!!");
						this.dispose();
					}
					else
						JOptionPane.showMessageDialog(null,"Error occured in replacing!!");
				}
				else
				{
					PreprocessPanel.report.append("No missing values Found<br>");
					PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
					PreprocessPanel.summary.repaint();
					JOptionPane.showMessageDialog(null,"Column doesnt contain missing values!! Cannot replace!!");
				}
			}
			
		}
	}

class Missing_Functions 
{
	DefaultTableModel dtm;
	Missing_Functions(DefaultTableModel dtm)
	{
		this.dtm=dtm;
	}
	
	boolean ContainsMissingValues()
	{
		if(dtm.getRowCount()>0)
		{
			for(int i=0;i<this.dtm.getRowCount();i++)
			{
				for(int j=0;j<this.dtm.getColumnCount();j++)
				{
					if(this.dtm.getValueAt(i,j).toString().trim().equals("?") || this.dtm.getValueAt(i,j).toString().trim().equals("") || this.dtm.getValueAt(i,j).toString().trim().equals(null))
					{
						// highlite
						return true;
					}
				}
			}
		}
		return false;	
	}
	int CountMissingValues()
	{
		int cnt=0;
		if(this.dtm.getRowCount()>0)
		{
			for(int i=0;i<this.dtm.getRowCount();i++)
			{
				for(int j=0;j<this.dtm.getColumnCount();j++)
				{
					if(this.dtm.getValueAt(i,j).toString().trim().equals("?") || this.dtm.getValueAt(i,j).toString().trim().equals("") || this.dtm.getValueAt(i,j).toString().trim().equals(null))
						cnt++;
				}
			}
		}
		return cnt;	
	}
	
	
}
